package com.linln.admin.pres.controller;

import com.linln.admin.pres.domain.PrescriptionsInfo;
import com.linln.admin.pres.service.PrescriptionsInfoService;
import com.linln.admin.pres.validator.PrescriptionsInfoValid;
import com.linln.common.enums.StatusEnum;
import com.linln.common.utils.EntityBeanUtil;
import com.linln.common.utils.ResultVoUtil;
import com.linln.common.utils.StatusUtil;
import com.linln.common.vo.ResultVo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author wh
 * @date 2024/03/27
 */
@Controller
@RequestMapping("/pres/")
public class PrescriptionsInfoController {

    @Autowired
    private PrescriptionsInfoService prescriptionsInfoService;

    /**
     * 列表页面
     */
    @GetMapping("/index")
    @RequiresPermissions("pres::index")
    public String index(Model model, PrescriptionsInfo prescriptionsInfo) {

        // 创建匹配器，进行动态查询匹配
        ExampleMatcher matcher = ExampleMatcher.matching()
                .withMatcher("name", match -> match.contains())
                .withMatcher("classification", match -> match.contains())
                .withMatcher("jfunction", match -> match.contains())
                .withMatcher("jattending", match -> match.contains())
                .withMatcher("zfunction", match -> match.contains())
                .withMatcher("zattending", match -> match.contains())
                .withMatcher("fangyi", match -> match.contains())
                .withMatcher("intro", match -> match.contains())
                .withMatcher("considerations", match -> match.contains())
                .withMatcher("jcomposition", match -> match.contains())
                .withMatcher("title", match -> match.contains())
                .withMatcher("description", match -> match.contains());

        // 获取数据列表
        Example<PrescriptionsInfo> example = Example.of(prescriptionsInfo, matcher);
        Page<PrescriptionsInfo> list = prescriptionsInfoService.getPageList(example);

        // 封装数据
        model.addAttribute("list", list.getContent());
        model.addAttribute("page", list);
        return "/pres//index";
    }

    /**
     * 跳转到添加页面
     */
    @GetMapping("/add")
    @RequiresPermissions("pres::add")
    public String toAdd() {
        return "/pres//add";
    }

    /**
     * 跳转到编辑页面
     */
    @GetMapping("/edit/{id}")
    @RequiresPermissions("pres::edit")
    public String toEdit(@PathVariable("id") PrescriptionsInfo prescriptionsInfo, Model model) {
        model.addAttribute("prescriptionsInfo", prescriptionsInfo);
        return "/pres//add";
    }

    /**
     * 保存添加/修改的数据
     * @param valid 验证对象
     */
    @PostMapping("/save")
    @RequiresPermissions({"pres::add", "pres::edit"})
    @ResponseBody
    public ResultVo save(@Validated PrescriptionsInfoValid valid, PrescriptionsInfo prescriptionsInfo) {
        // 复制保留无需修改的数据
        if (prescriptionsInfo.getId() != null) {
            PrescriptionsInfo bePrescriptionsInfo = prescriptionsInfoService.getById(prescriptionsInfo.getId());
            EntityBeanUtil.copyProperties(bePrescriptionsInfo, prescriptionsInfo);
        }

        // 保存数据
        prescriptionsInfoService.save(prescriptionsInfo);
        return ResultVoUtil.SAVE_SUCCESS;
    }

    /**
     * 爬取数据
     */
    @GetMapping("/crawl")
    @RequiresPermissions({"pres::add", "pres::edit"})
    @ResponseBody
    public ResultVo crawl(String keyword) {
        // 保存数据
        prescriptionsInfoService.crawlBySearch(keyword, 1);
        return ResultVoUtil.SAVE_SUCCESS;
    }

    /**
     * 统计线程
     */
    @GetMapping("/statisticByThread")
    @RequiresPermissions({"pres::add", "pres::edit"})
    @ResponseBody
    public ResultVo statisticByThread() {
        // 保存数据
        prescriptionsInfoService.statisticByThread();
        return ResultVoUtil.SAVE_SUCCESS;
    }

    /**
     * 爬取数据
     */
    @GetMapping("/crawlByThread")
    @RequiresPermissions({"pres::add", "pres::edit"})
    @ResponseBody
    public ResultVo crawlByThread(@RequestParam(defaultValue = "1130000") Integer no) throws InterruptedException {
        if (no < 1130000) {
            no = 1130000;
        }
        // 保存数据
        prescriptionsInfoService.crawlByThread(no);
        return ResultVoUtil.SAVE_SUCCESS;
    }

    /**
     * 跳转到详细页面
     */
    @GetMapping("/detail/{id}")
    @RequiresPermissions("pres::detail")
    public String toDetail(@PathVariable("id") PrescriptionsInfo prescriptionsInfo, Model model) {
        model.addAttribute("prescriptionsInfo",prescriptionsInfo);
        return "/pres//detail";
    }

    /**
     * 设置一条或者多条数据的状态
     */
    @RequestMapping("/status/{param}")
    @RequiresPermissions("pres::status")
    @ResponseBody
    public ResultVo status(
            @PathVariable("param") String param,
            @RequestParam(value = "ids", required = false) List<Integer> ids) {
        // 更新状态
        StatusEnum statusEnum = StatusUtil.getStatusEnum(param);
        if (prescriptionsInfoService.updateStatus(statusEnum, ids)) {
            return ResultVoUtil.success(statusEnum.getMessage() + "成功");
        } else {
            return ResultVoUtil.error(statusEnum.getMessage() + "失败，请重新操作");
        }
    }
}